$(document).ready(function(){ 


  /////////////////////////////////////          
	// Onload function
  /////////////////////////////////////	
  $.ajax({  
    type: "POST",  
    url: "index.php",  
    data: { module: "IShopStore", type: "ajax", func: "getCart"},  
    success: function(theResponse) {	
      if(theResponse == 0){
        $("#onloadCart").show("slow");						
        $("#onloadCart").html("<div>&nbsp;</div><div>ไม่มีสินค้าในตระกร้า</div><div>&nbsp;</div>");						
        $("#notificationsLoader").empty();      
      }else{
        $("#onloadCart").show("slow");						
        $("#onloadCart").html(theResponse); 
        $("#buttonCart").html($.buttonSideCart);                						
        $("#notificationsLoader").empty();
      }
    }  
  });//End onload function   



  /////////////////////////////////////          
	// Add item to cart when onclick
  /////////////////////////////////////	
  $(".productPriceWrapRight a button").click(function() {
  
		var productIDValSplitter 	= (this.id).split("_");
		var productFunctionVal = productIDValSplitter[1];
		var productIDVal 			= productIDValSplitter[2];

    //alert(this.id);
				
		var productX 		= $("#productImageWrapID_"+ productFunctionVal +"_"+ productIDVal).offset().left;
		var productY 		= $("#productImageWrapID_"+ productFunctionVal  +"_"+  productIDVal).offset().top;
		
		if( $("#productID_"+ productFunctionVal + productIDVal).length > 0){
			var basketX 		= $("#productID_"+ productFunctionVal +"_"+ productIDVal).offset().left;
			var basketY 		= $("#productID_"+ productFunctionVal +"_"+ productIDVal).offset().top;			
		} else {
			var basketX 		= $("#basketTitleWrap").offset().left;
			var basketY 		= $("#basketTitleWrap").offset().top;
		}	
				
		var gotoX 			= basketX - productX;
		var gotoY 			= basketY - productY;
		
		var newImageWidth 	= $("#productImageWrapID_"+ productFunctionVal +"_"+ productIDVal).width() / 3;
		var newImageHeight	= $("#productImageWrapID_"+ productFunctionVal +"_"+ productIDVal).height() / 3;

		$("#productImageWrapID_"+ productFunctionVal +"_"+ productIDVal + " img")
		.clone()
		.prependTo("#productImageWrapID_"+ productFunctionVal +"_"+ productIDVal)
		.css({'position' : 'absolute'})
		.animate({opacity: 0.4}, 100 )
		.animate({opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200, function() {
		  $(this).remove();		  
	   }); //End move image function   
    
    //Get thumbname
    var productThumbname = $("#productImageWrapID_"+ productFunctionVal +"_"+ productIDVal + " img").attr("src");

    //Get title
    var productTitle = $("#productTitle_"+ productFunctionVal +"_"+ productIDVal).text();  
    
    //Get price
    var productPrice = $("#price_"+ productFunctionVal +"_"+ productIDVal).text();        

    if ($("#productID_" + productIDVal).length > 0){
      $("#buttonCart").html($.buttonSideCart);         
      $("#productID_" + productIDVal).html('<img src="modules/IShopStore/pnjavascript/jbasket/images/loader.gif">');
    }else{
      $("#buttonCart").html($.buttonSideCart);         
      $("#notificationsLoader").html('<img src="modules/IShopStore/pnjavascript/jbasket/images/loader.gif">');    
    }          


    //Call addToCart function from php page
    var data = { 
                module: "IShopStore", 
                func: "addToCart", 
                type: "ajax", 
                id: productIDVal, 
                thumbname : productThumbname,
                name : productTitle,
                price : productPrice
               };        
    callbackFunction(data , function(callback){
      if(callback == 0){					
        $("#onloadCart").html("<div>&nbsp;</div><div>ไม่มีสินค้าในตระกร้า</div><div>&nbsp;</div>");						
        $("#notificationsLoader").empty();      
      }else{
       
        $("#onloadCart").show("slow");						
        $("#onloadCart").html(callback); 
        $("#buttonCart").html($.buttonSideCart);                         					
        $("#notificationsLoader").empty();	
      }
    }); //End callbackFunction for addToCart            
            					 
    /*
    testAjax(function(output){
      // here you use the output
      alert(output);
    });
    */
  });//End addCart function
          
          
             
  /////////////////////////////////////          
	// Delete from cart
  /////////////////////////////////////	
	$("#basketItemsWrap li button").live("click", function(event) { 

		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];	
		
		$("#productID_" + productIDVal).hide();		
    $("#notification_" + productIDVal).html('<img src="modules/IShopStore/pnjavascript/jbasket/images/loader.gif">');		
	
	
    //Call deleteFromCart function from php page	
    var data = {module: "IShopStore", func: "deleteFromCart", type: "ajax", id: productIDVal};            
    callbackFunction(data , function(callback){
      if(callback == 0){					
        $("#onloadCart").show("slow");						
        $("#onloadCart").html("<div>ไม่มีสินค้าในตระกร้า</div>");		
        $("#buttonCart").empty();          				
        $("#notificationsLoader").empty();     
      }else{

        $("#onloadCart").html(callback);
        $("#buttonCart").html($.buttonSideCart);             				
        $("#productID_" + productIDVal).remove();
        $("#notification_" + productIDVal).hide(2000000,  function() {$(this).remove();});        
      }
    }); //End callbackFunction for deleteFromCart

	});//End delete function
	
	
	//Normal function
	jQuery.buttonSideCart = function(){
    var buttonResponse = '<div>';
    buttonResponse+= '  <a href="#" class="big-link" data-reveal-id="myModal">';
    buttonResponse+= '    <button class="cart_button" type="submit">รายละเอียด</button>';
    buttonResponse+= '  </a>';
    buttonResponse+= '  <a href=index.php?module=IShopStore&type=user&func=checkout class="big-link">';
    buttonResponse+= '    <button class="cart_button" type="submit" value="Checkout" >Checkout</button>';
    buttonResponse+= '  </a>';
    buttonResponse+= '</div>';
    buttonResponse+= '<div>&nbsp;</div>';
    
    return 	buttonResponse;
	
	}
	
	//Callback function
  callbackFunction = function(param, callback){
  
    //alert("Call this");
    $.ajax({  
            type: "POST",  
            url: "index.php",  
            data: param,   
            success: callback 
    });  
  }

  //Callback function not JQuery at all
  function testAjax(handleData) {
    $.ajax({
      url:"index.php",  
      success:function(data) {
        handleData(data); 
      }
    });
  } 	
  
});

